package net.ciklum.svnnotify;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SVNNotifyActivity extends Activity {

	private static final int HELLO_ID = 1;
	private List<String> items = new ArrayList<String>();
	private static NotifListAdapter adapter = null;
	
	public static final long ALARM_INTERVAL = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
	public static final long ALARM_TRIGGER_AT_TIME = SystemClock
			.elapsedRealtime() + 30000;

	private String uri = "http://interns.googlecode.com/svn/trunk/interns/svn_notify";
	private String name = "ElenaKozina503@gmail.com";
	private String password = "Bn6PW5Vp9KU4";
	private String repoName = "interns";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// runAlarmManager();
		Intent serviceIntent = new Intent(SVNNotifyActivity.this,
				SVNNotifyService.class);
		serviceIntent.putExtra(SVNNotifyService.EXTRA_URI, uri);
		serviceIntent.putExtra(SVNNotifyService.EXTRA_LOGIN, name);
		serviceIntent.putExtra(SVNNotifyService.EXTRA_PASSWORD, password);
		startService(serviceIntent);

		createServiceNotifications();

		items.add("Crated TextView");
		items.add("Fixed bugs.");
		items.add("UI");
		items.add("Added notifications list.");
		items.add("1");
		items.add("2");
		items.add("3");
		items.add("4");
		items.add("5");
		items.add("6");
		items.add("7");
		items.add("8");
		items.add("9");
		items.add("10");
		items.add("11");
		items.add("12");

		adapter = new NotifListAdapter(this, items);

		ListView notifList = (ListView) findViewById(R.id.notif_list);
		notifList.setAdapter(adapter);
		notifList.setTextFilterEnabled(true);
		notifList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long arg3) {
				String selectedMenuItem = items.get((int) position);

				Intent intent = new Intent();
				intent.putExtra("title", selectedMenuItem);
				intent.setClass(SVNNotifyActivity.this, NotificationActivity.class);
				startActivity(intent);
			}
		});
	}

	private void runAlarmManager() {
		AlarmManager alarmMgr = (AlarmManager) this
				.getSystemService(Context.ALARM_SERVICE);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
				new Intent(this, AlarmReciever.class),
				PendingIntent.FLAG_CANCEL_CURRENT);
		alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
				ALARM_TRIGGER_AT_TIME, ALARM_INTERVAL, pendingIntent);
	}

	private class AlarmReciever extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Intent serviceIntent = new Intent(SVNNotifyActivity.this,
					SVNNotifyService.class);
			serviceIntent.putExtra("uri", uri);
			serviceIntent.putExtra("login", name);
			serviceIntent.putExtra("password", password);
			startService(serviceIntent);
		}
	};
	
	private void createServiceNotifications() {
		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		int icon = R.drawable.icon;
		CharSequence tickerText = "Hello";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		
		Context context = getApplicationContext();
		CharSequence contentTitle = "My notification";
		CharSequence contentText = "Hello World!";
		Intent notificationIntent = new Intent(this, SVNNotifyActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

		mNotificationManager.notify(HELLO_ID, notification);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.svn_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settins:
			startActivity(new Intent(this, SettingsActivity.class));
			return true;
		case R.id.menu_reps:
			viewRepsList();
			return true;
		case R.id.menu_clean:
			cleanNotifies();
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void cleanNotifies() {
		items.clear();
		adapter.notifyDataSetChanged();
	}

	private void viewRepsList() {
		Intent intent = new Intent();
		intent.setClass(SVNNotifyActivity.this, RepositoriesActivity.class);
		startActivity(intent);
	}

	private class NotifListWrapper {
		private TextView title = null;
		private TextView date = null;
		private View row = null;

		public NotifListWrapper(View row) {
			this.row = row;
		}

		public void populateFrom(String r) {
			getTitle().setText(r);
			getDate().setText(Calendar.getInstance().getTime().toString());
		}

		TextView getTitle() {
			if (title == null) {
				title = (TextView) row.findViewById(R.id.text1);
			}
			return (title);
		}

		TextView getDate() {
			if (date == null) {
				date = (TextView) row.findViewById(R.id.text2);
			}
			return (date);
		}

	}

	private class NotifListAdapter extends ArrayAdapter<String> {

		private Activity activity;
		private List<String> items;

		public NotifListAdapter(Activity activity, List<String> items) {
			super(activity, android.R.layout.simple_list_item_1, items);
			this.items = items;
			this.activity = activity;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			NotifListWrapper wrapper = null;
			if (row == null) {
				LayoutInflater inflater = activity.getLayoutInflater();
				row = inflater.inflate(R.layout.list_item2, null);
				wrapper = new NotifListWrapper(row);
				row.setTag(wrapper);

			} else {
				wrapper = (NotifListWrapper) row.getTag();
			}

			wrapper.populateFrom(items.get(position));
			return (row);
		}

	}

}